package com.ejd.web.vo.goods.detail;

import java.awt.Dimension;

import java.io.IOException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.application.FacesMessage;
import javax.faces.application.ViewHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.cglib.beans.BeanCopier;

import com.ejd.common.config.ConfigManager;
import com.ejd.controller.LoginController;
import com.ejd.service.ICodeMasterService;
import com.ejd.service.IColorService;
import com.ejd.service.IConsultationService;
import com.ejd.service.ICustomerService;
import com.ejd.service.IGoodsService;
import com.ejd.utils.DESPlusUtil;
import com.ejd.utils.DateTimeUtil;
import com.ejd.utils.MessageFactory;
import com.ejd.utils.SendHtmlMail;
import com.ejd.utils.SortManagerDelegate;
import com.ejd.utils.SpringFacesUtil;
import com.ejd.utils.WebPageData;
import com.ejd.web.bo.impl.CodeMaster;
import com.ejd.web.bo.impl.Color;
import com.ejd.web.bo.impl.Consultation;
import com.ejd.web.bo.impl.Customer;
import com.ejd.web.bo.impl.Goods;
import com.ejd.web.bo.impl.GoodsProperties;
import com.ejd.web.constant.EjdConstants;
import com.ejd.web.constant.ManageBeanConstants;
import com.ejd.web.constant.SpringBeanConstants;
import com.ejd.web.vo.common.ColorVo;
import com.ejd.web.vo.common.ConsultationPage;
import com.ejd.web.vo.common.ConsultationVo;
import com.ejd.web.vo.common.CustomerVo;
import com.ejd.web.vo.common.GoodsPropertiesVo;
import com.ejd.web.vo.common.GoodsVo;
import com.ejd.web.vo.customer.LoginCustomerPageBean;
import com.ejd.web.vo.genl.GenerateVerifyCodeBean;
@ManagedBean(name="goodsDetailPage")
@ViewScoped

public class GoodsDetailPageBean implements java.io.Serializable {
	private static final long serialVersionUID = -8106629465344224713L;
	private String primaryCategoryCode;
	private String code;
	private String productId;
	private GoodsVo goods;
	private String selectedColor;
	private List<String> colors = new ArrayList<String>();
	private String selectedSize;
	private List<String> sizes = new ArrayList<String>();
	private List<GoodsPropertiesVo> forSelectGoodsProperties = new ArrayList<GoodsPropertiesVo>();
	private Integer buyNumber;
	private SelectItem[] buyNumberItems;//for number for buy
	private Integer maxBuyNumber;
	private ConsultationPage aPage;
	private ConsultationPage gPage;
	private ConsultationPage sPage;
	private ConsultationPage lPage;
	private ConsultationPage pPage;
	private ConsultationVo consultation= new ConsultationVo();
	private String verifyCodeForQuestion;
	private String verifyCode;
	private String saveOutMessage;
	
	private String viewTab;
	
	private String sessionId;

	public String getPrimaryCategoryCode() {
		return primaryCategoryCode;
	}
	public void setPrimaryCategoryCode(String primaryCategoryCode) {
		this.primaryCategoryCode = primaryCategoryCode;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	
	public String getProductId() {
		return productId;
	}
	public void setProductId(String productId) {
		this.productId = productId;
	}
	public GoodsVo getGoods() {
		return goods;
	}
	public void setGoods(GoodsVo goods) {
		this.goods = goods;
	}
	
	public String getSelectedColor() {
		return selectedColor;
	}
	public void setSelectedColor(String selectedColor) {
		this.selectedColor = selectedColor;
	}
	
	public ConsultationPage getaPage() {
		return aPage;
	}
	public void setaPage(ConsultationPage aPage) {
		this.aPage = aPage;
	}
	public ConsultationPage getgPage() {
		return gPage;
	}
	public void setgPage(ConsultationPage gPage) {
		this.gPage = gPage;
	}
	public ConsultationPage getsPage() {
		return sPage;
	}
	public void setsPage(ConsultationPage sPage) {
		this.sPage = sPage;
	}
	public ConsultationPage getlPage() {
		return lPage;
	}
	public void setlPage(ConsultationPage lPage) {
		this.lPage = lPage;
	}
	public ConsultationPage getpPage() {
		return pPage;
	}
	public void setpPage(ConsultationPage pPage) {
		this.pPage = pPage;
	}
	
	public ConsultationVo getConsultation() {
		return consultation;
	}
	public void setConsultation(ConsultationVo consultation) {
		this.consultation = consultation;
	}
	
	public String getVerifyCodeForQuestion() {
		return verifyCodeForQuestion;
	}
	public void setVerifyCodeForQuestion(String verifyCodeForQuestion) {
		this.verifyCodeForQuestion = verifyCodeForQuestion;
	}
	
	public String getVerifyCode() {
		return verifyCode;
	}
	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}
	
	public String getSaveOutMessage() {
		return saveOutMessage;
	}
	public void setSaveOutMessage(String saveOutMessage) {
		this.saveOutMessage = saveOutMessage;
	}
	
	public String getViewTab() {
		return viewTab;
	}
	public void setViewTab(String viewTab) {
		this.viewTab = viewTab;
	}
	
	public String getSessionId() {
		return sessionId;
	}
	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}
	public String getShowImage(){
		String selectedColor = this.getSelectedColor();
		String mapKey = this.getGoods().getCode() + selectedColor;
		Map<String,String> allMapShowImages = this.getGoods().getVoPropertiesMapShowImages();
		return allMapShowImages.get(mapKey);
	}
	
	public List<String> getShowImageList() {
		String selectedColor = this.getSelectedColor();
		String mapKey = this.getGoods().getCode() + selectedColor;
		Map<String,List<String>> allMapImages = this.getGoods().getVoPropertiesMapImages();
		return allMapImages.get(mapKey);
	}
	
	
	
	/*private String getImageName() {//用于没有多个属性的值 image直接存储在主数据中
		String result = "";
		if (null == this.getGoods().getPropertiesv() || this.getGoods().getPropertiesv().isEmpty()) {
			result = this.getGoods().getImage1();
		}
		return result;
	}
	private Map<String,String> getPropertiesMapShowImages() {//用于有多个属性的值 image直接存储在附加属性中,显示第1幅图片的
		Map result = new HashMap<String,String>();
		if (null != this.getGoods().getPropertiesv() || !(this.getGoods().getPropertiesv().isEmpty())) {
			for (GoodsPropertiesVo property:this.getGoods().getPropertiesv()) {
				String mapKey = this.getCode()+property.getColor();
				result.put(mapKey, property.getSfield1());
			}
		}
		return result;
	}
	
	private Map<String,List<String>> getPropertiesMapImages() {//用于有多个属性的值 image直接存储在附加属性中,显示第1幅图片的
		Map result = new HashMap<String,List<String>>();
		if (null != this.getGoods().getPropertiesv() || !(this.getGoods().getPropertiesv().isEmpty())) {
			for (GoodsPropertiesVo property:this.getGoods().getPropertiesv()) {
				List<String> imageList = new ArrayList<String>();
				imageList.add(property.getSfield1());
				imageList.add(property.getSfield2());
				imageList.add(property.getSfield3());
				imageList.add(property.getSfield4());
				imageList.add(property.getSfield5());
				imageList.add(property.getSfield6());
				imageList.add(property.getSfield7());
				imageList.add(property.getSfield8());
				imageList.add(property.getSfield9());
				imageList.add(property.getSfield10());
				String mapKey = this.getCode()+property.getColor();
				result.put(mapKey, imageList);
			}
		}
		return result;
	}*/
	
	
	public String getSelectedColorIn18() {
		String result = "";
		if (null == this.getSelectedColor() || "".equals(this.getSelectedColor())) {
			return result;
		}
		IColorService<Color,Long> colorService = (IColorService<Color,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.COLOR_SERVICE_BEAN_NAME);
		colorService.setEntityClass(Color.class);
		colorService.setPKClass(Long.class);
		Color color = colorService.findUniqueBy("colorId", this.selectedColor);
		if (null == color) {
			return result;
		} else {
			String colorNameN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,color.getColorName(), FacesMessage.SEVERITY_ERROR).getDetail();
			result = colorNameN18;
		}
		return result;
	}
	public List<String> getColors() {
		return colors;
	}
	public void setColors(List<String> colors) {
		this.colors = colors;
	}
	public List<ColorVo> getColorsWhitIn18() {
		List<ColorVo> result = new ArrayList<ColorVo>();
		if (null != this.getColors() && !(this.getColors().isEmpty())) {
			IColorService<Color,Long> colorService = (IColorService<Color,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.COLOR_SERVICE_BEAN_NAME);
			colorService.setEntityClass(Color.class);
			colorService.setPKClass(Long.class);
			for (String colorId:this.getColors()) {
				Color color = colorService.findUniqueBy("colorId", colorId);
				ColorVo colorVo = new ColorVo();
				BeanCopier copyColor = BeanCopier.create(Color.class, ColorVo.class, false);
				copyColor.copy(color, colorVo, null);
				String tempColorNameIn18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,color.getColorName(), FacesMessage.SEVERITY_ERROR).getDetail();
				colorVo.setColorNameIn18(tempColorNameIn18);
				result.add(colorVo);
			}
		}
		return result;
	}
	public String getSelectedSize() {
		return selectedSize;
	}
	public void setSelectedSize(String selectedSize) {
		this.selectedSize = selectedSize;
	}
	public List<String> getSizes() {
		return sizes;
	}
	public void setSizes(List<String> sizes) {
		this.sizes = sizes;
	}
	
	public List<GoodsPropertiesVo> getForSelectGoodsProperties() {
		return forSelectGoodsProperties;
	}
	public void setForSelectGoodsProperties(List<GoodsPropertiesVo> forSelectGoodsProperties) {
		this.forSelectGoodsProperties = forSelectGoodsProperties;
	}
	
	public Integer getBuyNumber() {
		return buyNumber;
	}
	public void setBuyNumber(Integer buyNumber) {
		this.buyNumber = buyNumber;
	}
	
	public SelectItem[] getBuyNumberItems() {
		return buyNumberItems;
	}
	public void setBuyNumberItems(SelectItem[] buyNumberItems) {
		this.buyNumberItems = buyNumberItems;
	}
	
	public Integer getMaxBuyNumber() {
		return maxBuyNumber;
	}
	public void setMaxBuyNumber(Integer maxBuyNumber) {
		this.maxBuyNumber = maxBuyNumber;
	}
	public String getShowImageName() {
		String result = "noImage.jpg";
		String imageNameOfDb = this.getGoods().getImageName();
		String name = imageNameOfDb.substring(0,imageNameOfDb.indexOf("."));
		result = name + "-" + this.getSelectedColor()+ ".jpg";
		return result;
	}
	
	public GoodsDetailPageBean() throws IOException {
		String primaryCategoryCode = (String) SpringFacesUtil.getRequestParameter("primaryCategoryCode");
		primaryCategoryCode = DESPlusUtil.decrypt(primaryCategoryCode);
		String code = (String) SpringFacesUtil.getRequestParameter("code");
		code = DESPlusUtil.decrypt(code);
		String productId = (String) SpringFacesUtil.getRequestParameter("productId");
		productId = DESPlusUtil.decrypt(productId);
		String sessionId = (String) SpringFacesUtil.getRequestParameter("sessionId");
		String enSessionId = DESPlusUtil.decrypt(sessionId);
		String currentSessionId = SpringFacesUtil.getCurrentSessionId();
		if (null == sessionId || "".equals(sessionId) || null == currentSessionId || "".equals(currentSessionId) || !(enSessionId.equals(currentSessionId))) {
			SpringFacesUtil.redirectPage(EjdConstants.INDEX_HTML);
		}
		this.setSessionId(sessionId);
		String selectedColor =(String) SpringFacesUtil.getRequestParameter("specialField1");
		if (null != selectedColor && !("".equals(selectedColor))) {
			selectedColor = DESPlusUtil.decrypt(selectedColor);
		}
		String selectedSize =(String) SpringFacesUtil.getRequestParameter("specialField2");
		if (null != selectedSize && !("".equals(selectedSize))) {
			selectedSize = DESPlusUtil.decrypt(selectedSize);
		}
		if (null == primaryCategoryCode || "".equals(primaryCategoryCode) || "null".equals(primaryCategoryCode)
				||null == code || "".equals(code) || "null".equals(code)
						|| null == productId || "".equals(productId) || "null".equals(productId)) {
			SpringFacesUtil.redirectPage(EjdConstants.INDEX_HTML);
		} else {
			this.setPrimaryCategoryCode(primaryCategoryCode);
			this.setCode(code);
			this.setProductId(productId);
			this.setSelectedColor(selectedColor);
			this.setSelectedSize(selectedSize);
			this.setBuyNumber(1);
			this.stuffOneBuyNumber();// one number for buy,default;
			IGoodsService<Goods,Long> goodsService = (IGoodsService<Goods,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.GOODS_SERVICE_BEAN_NAME);
			goodsService.setEntityClass(Goods.class);
			goodsService.setPKClass(Long.class);
			Goods currGoods = goodsService.findUniqueBy("code", code);
			BeanCopier copyGoods = BeanCopier.create(Goods.class, GoodsVo.class, false);
			GoodsVo goodsVo = new GoodsVo();
			copyGoods.copy(currGoods, goodsVo, null);
			Set<GoodsProperties> currGoodsProperties = currGoods.getProperties();
			List<GoodsPropertiesVo> pgoodsProperties =new ArrayList<GoodsPropertiesVo>();
			if (null !=currGoodsProperties && !(currGoodsProperties.isEmpty())) {
				Iterator<GoodsProperties> iterator = currGoodsProperties.iterator();
				while(iterator.hasNext()) {
					GoodsProperties tempGoodsProperties = iterator.next();
					GoodsPropertiesVo tempGoodsPropertiesVo = new GoodsPropertiesVo();
					BeanCopier copyHere = BeanCopier.create(GoodsProperties.class,GoodsPropertiesVo.class,false);
					copyHere.copy(tempGoodsProperties, tempGoodsPropertiesVo,null);
					pgoodsProperties.add(tempGoodsPropertiesVo);
				}
			}
			goodsVo.setPropertiesv(pgoodsProperties);
			//如果properties不为空,且selected Color 为空 ,则默认选种第1个颜色
			if (null != goodsVo.getPropertiesv() && !(goodsVo.getPropertiesv().isEmpty()) && (null == this.getSelectedColor() || "".equals(this.getSelectedColor()))) {
				this.setSelectedColor(goodsVo.getPropertiesv().get(0).getColor());
			}
			this.setGoods(goodsVo);
			List<GoodsPropertiesVo> tgoodsProperties =new ArrayList<GoodsPropertiesVo>();
			tgoodsProperties = goodsVo.getPropertiesv();
			List<String> tempColors = new ArrayList<String>();
			if (null != tgoodsProperties && !(tgoodsProperties.isEmpty())) {
				for (GoodsPropertiesVo goodsPropertiesVo:tgoodsProperties) {
					if (tempColors.contains(goodsPropertiesVo.getColor())) {
						continue;
					} else {
						tempColors.add(goodsPropertiesVo.getColor());
					}
				}
				
			}
			this.setColors(tempColors);
			//set default color and for select shoes properties
			if (null != tempColors && tempColors.size() > 0) {
				String color = tempColors.get(0);
				if (null == this.getSelectedColor()) {
					this.setSelectedColor(color);
				}
				List<GoodsPropertiesVo> theGoodsProperties = this.getGoods().getPropertiesv();
				List<GoodsPropertiesVo> tempGoodsProperties = new ArrayList<GoodsPropertiesVo>();
				for (GoodsPropertiesVo goodsPropertiesVo:theGoodsProperties) {
					if (color.equals(goodsPropertiesVo.getColor())) {
					tempGoodsProperties.add(goodsPropertiesVo);
					}
				}
				SortManagerDelegate<GoodsPropertiesVo> sortManageDelegate = new SortManagerDelegate<GoodsPropertiesVo>();
				sortManageDelegate.setDatas(tempGoodsProperties);
				sortManageDelegate.setSortByProperty("size");
				this.setForSelectGoodsProperties(sortManageDelegate.findAsc());
			}
			//end 
			// GenerateVerifyCode
			GenerateVerifyCodeBean generateVerifyCode = (GenerateVerifyCodeBean) SpringFacesUtil.getManagedBean(ManageBeanConstants.GENERATE_VERIFY_CODE_BEAN);
			this.setVerifyCodeForQuestion(generateVerifyCode.getVerifyCode());
			// end generateVerifyCode
			//init gPage
			Consultation paraA = new Consultation();
			paraA.setCode(code);
			paraA.setStatusCode("O");
			paraA.setShowOnPage(true);
			aPage = new ConsultationPage(1,5,paraA);
			aPage.getPageDatas();
			Consultation paraG = new Consultation();
			paraG.setCode(code);
			paraG.setTypeCode("G");
			paraG.setStatusCode("O");
			paraG.setShowOnPage(true);
			gPage  = new ConsultationPage(1,5,paraG);
			gPage.getPageDatas();
			//start spage
			Consultation paraS = new Consultation();
			paraS.setCode(code);
			paraS.setTypeCode("S");
			paraS.setStatusCode("O");
			paraS.setShowOnPage(true);
			sPage  = new ConsultationPage(1,5,paraS);
			sPage.getPageDatas();
			//end spage
			//start spage
			Consultation paraL = new Consultation();
			paraL.setCode(code);
			paraL.setTypeCode("L");
			paraL.setStatusCode("O");
			paraL.setShowOnPage(true);
			lPage  = new ConsultationPage(1,5,paraL);
			lPage.getPageDatas();
			//end spage
			//start spage
			Consultation paraP = new Consultation();
			paraP.setCode(code);
			paraP.setTypeCode("P");
			paraP.setStatusCode("O");
			paraP.setShowOnPage(true);
			pPage  = new ConsultationPage(1,5,paraP);
			pPage.getPageDatas();
			//end spage
			this.setViewTab(EjdConstants.CONSULTATION_VIEW_TAB_ALL);
		}
	}
	
	public void goDetailPage(ActionEvent evt) {
		String code =(String) SpringFacesUtil.getRequestParameter("code");
		this.setCode(code);
	}
	/*public String goDetailPage(){
		if (this.checkJspExist(this.getCode()+".xhtml", "/layout/goods/shoes") && this.checkJspExist(this.getCode()+"Detail.xhtml", "/layout/goods/shoes/sub") ) {
			return this.getCode() + "?faces-redirect=true&primaryCategoryCode=" + DESPlusUtil.encrypt(this.getPrimaryCategoryCode()) + "&code=" + DESPlusUtil.encrypt(this.getCode()) + "&productId=" + DESPlusUtil.encrypt(this.getProductId());
		} else {
			return "/WEB-INF/" + EjdConstants.NO_PRODUCT_DETAIL_PAGE;
		}
	}*/
	
	public String selectColor() {
		String color =(String) SpringFacesUtil.getRequestParameter("color");
		this.setSelectedColor(color);
		List<GoodsPropertiesVo> theGoodsProperties = this.getGoods().getPropertiesv();
		List<GoodsPropertiesVo> tempGoodsProperties = new ArrayList<GoodsPropertiesVo>();
		for (GoodsPropertiesVo goodsPropertiesVo:theGoodsProperties) {
			if (color.equals(goodsPropertiesVo.getColor())) {
			tempGoodsProperties.add(goodsPropertiesVo);
			}
		}
		SortManagerDelegate<GoodsPropertiesVo> sortManageDelegate = new SortManagerDelegate<GoodsPropertiesVo>();
		sortManageDelegate.setDatas(tempGoodsProperties);
		sortManageDelegate.setSortByProperty("size");
		this.setForSelectGoodsProperties(sortManageDelegate.findAsc());
		//judge whether reset selectSize;
		boolean hasSameSize = false;
		if (null != this.getSelectedSize() && !("".equals(this.getSelectedSize()))) {
			if (null != tempGoodsProperties && !(tempGoodsProperties.isEmpty())) {
				for (GoodsPropertiesVo tempGoodsProperty:tempGoodsProperties) {
					if (hasSameSize) {
						break;
					}
					if (tempGoodsProperty.getSize().equals(this.getSelectedSize())) {
						hasSameSize = true;
					}
				}
			}
		}
		if (!hasSameSize) {
			this.setSelectedSize("");
		}
		//end
		this.stuffOneBuyNumber();// one number for buy,default;
		return null;
	}
	
	public String selectSize() {
		String size =(String) SpringFacesUtil.getRequestParameter("size");
		this.setSelectedSize(size);
		this.stuffBuyNumberOptions(this.getSelectedSize(), this.getForSelectGoodsProperties());
		return null;
	}
	private void stuffBuyNumberOptions(String size, List<GoodsPropertiesVo> forSelectGoodsProperties ) {
		if (null == size || "".equals(size)) {
			return ;
		}
		if (null == forSelectGoodsProperties || !(forSelectGoodsProperties.size() >=1)) {
			return ;
		}
		Integer buyNumber = 1;
		Integer goodsNumber = 1;
		for (GoodsPropertiesVo tempGoodsProperty:forSelectGoodsProperties) {
			if (size.equals(tempGoodsProperty.getSize())) {
				goodsNumber = tempGoodsProperty.getNumber();
				break;
			}
		}
		Integer customerPermissionBuyNumber = EjdConstants.MAX_NUMBER_FOR_BUY_NUMBER;
		ICodeMasterService<CodeMaster,Long> codeMasterService = (ICodeMasterService<CodeMaster,Long>)SpringFacesUtil.getManagedBean(SpringBeanConstants.CODE_MASTER_SERVICE_BEAN_NAME);
		codeMasterService.setEntityClass(CodeMaster.class);
		codeMasterService.setPKClass(Long.class);
		CodeMaster codeMaster = (CodeMaster) codeMasterService.findUniqueBy("masterCode", EjdConstants.MAX_NUMBER_FOR_BUY_CODE_MASTER);
		if (null != codeMaster && null != codeMaster.getiField1()) {
			customerPermissionBuyNumber = codeMaster.getiField1();
		}
		if (goodsNumber.compareTo(customerPermissionBuyNumber) <= 0) {
			buyNumber = goodsNumber ;
		} else {
			buyNumber = customerPermissionBuyNumber;
		}
		SelectItem[] tempBuyNumberItems = new SelectItem[buyNumber.intValue()];
		for (int i= 1; i <= buyNumber; i++) {
			tempBuyNumberItems[i-1] = new SelectItem(new Integer(i),String.valueOf(i));
		}
		this.setBuyNumberItems(tempBuyNumberItems);
		this.setMaxBuyNumber(buyNumber);
		//set default buy number 1
		this.setBuyNumber(1);
	}
	
	private void stuffOneBuyNumber() {//default data only 1
		Integer buyNumber = 1;
		SelectItem[] tempBuyNumberItems = new SelectItem[buyNumber];
		for (int i= 1; i <= buyNumber; i++) {
			tempBuyNumberItems[i-1] = new SelectItem(new Integer(i),String.valueOf(i));
		}
		this.setBuyNumberItems(tempBuyNumberItems);
		this.setMaxBuyNumber(buyNumber);
	}

	public void pageNoChangeOfgPage(ValueChangeEvent event) {
		String newValue = (String) event.getNewValue().toString();
		this.getgPage().setPageNo(Integer.valueOf(newValue));
		System.out.println(newValue);
		this.getgPage().getPageDatas();
	}
	public String selectaPageNo() {
		String result = null;
		String pageNo = (String) SpringFacesUtil.getRequestParameter("pageNo");
		if (this.getaPage().getPageNo().intValue() == Integer.valueOf(pageNo).intValue()) {
			return result;
		}
		this.getaPage().setPageNo(Integer.valueOf(pageNo).intValue());
		this.getaPage().getPageDatas();
		return result;
		
	}
	public String selectgPageNo() {
		String result = null;
		String pageNo = (String) SpringFacesUtil.getRequestParameter("pageNo");
		if (this.getgPage().getPageNo().intValue() == Integer.valueOf(pageNo).intValue()) {
			return result;
		}
		this.getgPage().setPageNo(Integer.valueOf(pageNo).intValue());
		this.getgPage().getPageDatas();
		return result;
		
	}
	public String selectsPageNo() {
		String result = null;
		String pageNo = (String) SpringFacesUtil.getRequestParameter("pageNo");
		if (this.getsPage().getPageNo().intValue() == Integer.valueOf(pageNo).intValue()) {
			return result;
		}
		this.getsPage().setPageNo(Integer.valueOf(pageNo).intValue());
		this.getsPage().getPageDatas();
		return result;
		
	}
	public String selectlPageNo() {
		String result = null;
		String pageNo = (String) SpringFacesUtil.getRequestParameter("pageNo");
		if (this.getlPage().getPageNo().intValue() == Integer.valueOf(pageNo).intValue()) {
			return result;
		}
		this.getlPage().setPageNo(Integer.valueOf(pageNo).intValue());
		this.getlPage().getPageDatas();
		return result;
		
	}
	public String selectpPageNo() {
		String result = null;
		String pageNo = (String) SpringFacesUtil.getRequestParameter("pageNo");
		if (this.getpPage().getPageNo().intValue() == Integer.valueOf(pageNo).intValue()) {
			return result;
		}
		this.getpPage().setPageNo(Integer.valueOf(pageNo).intValue());
		this.getpPage().getPageDatas();
		return result;
		
	}
	public String generateVerifyCode() {
		GenerateVerifyCodeBean generateVerifyCode = (GenerateVerifyCodeBean) SpringFacesUtil.getManagedBean(ManageBeanConstants.GENERATE_VERIFY_CODE_BEAN);
		this.setVerifyCodeForQuestion(generateVerifyCode.getVerifyCode());
		return null;
	}
	public String changeBuyNumber(ValueChangeEvent event) {
		String newValue = event.getNewValue().toString();
		this.setBuyNumber(new Integer(newValue));
		return null;
	}
	public String saveConsultation(){
		this.setNullOfSaveOutMessage();
		String code = (String)SpringFacesUtil.getRequestParameter("code");
		String name = (String)SpringFacesUtil.getRequestParameter("name");
		String primaryCategoryCode = (String)SpringFacesUtil.getRequestParameter("primaryCategoryCode");
		String secondCategoryCode = (String)SpringFacesUtil.getRequestParameter("secondCategoryCode");
		String productId = (String)SpringFacesUtil.getRequestParameter("productId");
		String statusCode = EjdConstants.CONSULTATION_STATUS_CODE_NEW;
		boolean showOnPage = false;
		if (!(LoginController.loggedIn())) {
			return EjdConstants.GO_TO_LOGIN + "?faces-redirect=true";
		}
		Consultation newConsultation = new Consultation();
		this.getConsultation().setCode(code);
		this.getConsultation().setName(name);
		this.getConsultation().setPrimaryCategoryCode(primaryCategoryCode);
		this.getConsultation().setSecondCategoryCode(secondCategoryCode);
		this.getConsultation().setProductId(new Long(productId).longValue());
		this.getConsultation().setStatusCode(statusCode);
		this.getConsultation().setShowOnPage(showOnPage);
		BeanCopier copyHere = BeanCopier.create(ConsultationVo.class,Consultation.class,false);
		copyHere.copy(this.getConsultation(), newConsultation, null);
		Customer customer = new Customer();
		/*ICustomerService<Customer,Long> customerService = (ICustomerService<Customer,Long>) SpringFacesUtil.getSpringBean(SpringBeanConstants.CUSTOMER_SERVICE_BEAN_NAME);
		customerService.setEntityClass(Customer.class);
		customerService.setPKClass(Long.class);*/
		LoginCustomerPageBean loginCustomerPage = (LoginCustomerPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.LOGIN_CUSTOMER_PAGE_BEAN_NAME);
		customer = loginCustomerPage.getCustomerInfo();
		newConsultation.setCustomer(customer);
		newConsultation.setAskTime(DateTimeUtil.getCurrentTimestamp());
		IConsultationService<Consultation,Long> consultationService = (IConsultationService<Consultation,Long>) SpringFacesUtil.getSpringBean(SpringBeanConstants.CONSULTATION_SERVICE_BEAN_NAME);
		consultationService.setEntityClass(Consultation.class);
		consultationService.setPKClass(Long.class);
		try{
			consultationService.save(newConsultation);
			FacesMessage message;
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "consultation.save.success.out.message", FacesMessage.SEVERITY_ERROR, customer.getName());
			this.setSaveOutMessage(message.getDetail());
		} catch (Exception e) {
			FacesMessage message;
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "consultation.save.fail.out.message", FacesMessage.SEVERITY_ERROR, customer.getName());
			this.setSaveOutMessage(message.getDetail());
            throw new InternalError(
                " Runtime Exception impossibility throw ");
        }
		GenerateVerifyCodeBean generateVerifyCode = (GenerateVerifyCodeBean) SpringFacesUtil.getManagedBean(ManageBeanConstants.GENERATE_VERIFY_CODE_BEAN);
		this.setVerifyCodeForQuestion(generateVerifyCode.getVerifyCode());
		return null;
	}
	private void setNullOfSaveOutMessage(){
		this.setSaveOutMessage(null);
	}
	public String selectTab(){
		String tabNumber = (String)SpringFacesUtil.getRequestParameter("tabNumber");
		System.out.println(tabNumber);
		return null;
	}
	public String selectConsultationViewTab(){
		String consultationViewTab = (String)SpringFacesUtil.getRequestParameter("consultationViewTab");
		if (null != consultationViewTab && !("".equals(consultationViewTab))) {
			this.setViewTab(consultationViewTab);
		} else {
			this.setViewTab(EjdConstants.CONSULTATION_VIEW_TAB_ALL);
		}
		return null;
	}
	
	public String shareToFriend(String primaryCategoryCode, String code, String id) throws Exception {
		primaryCategoryCode = DESPlusUtil.encrypt(primaryCategoryCode);
		code = DESPlusUtil.encrypt(code);
		id = DESPlusUtil.encrypt(id);
		ConfigManager configer = ConfigManager.getInstance();
		String website = configer.getProperty("web.site");
		boolean useProxy = new Boolean(configer.getProperty("url.useProxy")).booleanValue();
	    String urlHost = configer.getProperty("url.host");
	    int urlPort = new Integer(configer.getProperty("url.port")).intValue();
	    String urlUsername = configer.getProperty("url.username");
	    String urlPassword = configer.getProperty("url.password");
		String urlContent = "";
		WebPageData webPage = new WebPageData(useProxy,urlPort,urlHost,urlUsername,urlPassword);
		String viewId = SpringFacesUtil.getViewId();
		FacesContext fc = SpringFacesUtil.getFacesContext();
		String urlStr = website + fc.getExternalContext().getRequestContextPath() + viewId + "?" + "primaryCategoryCode=" + primaryCategoryCode + "&code=" + code + "&productId=" + id;
		urlContent = webPage.sendUrl(urlStr, null);
		String mailTo = "jqhero79@gmail.com";
		String mailFrom = configer.getProperty("email.from");
		String smtpServer = configer.getProperty("email.host");
		String mailPort = configer.getProperty("email.port");
		String mailProxySet = configer.getProperty("email.proxySet");
		String mailSocksProxyHost = configer.getProperty("email.socksProxyHost");
		String mailSocksProxyPort = configer.getProperty("email.socksProxyPort");
		String mailUserName = configer.getProperty("email.username");
		String mailPassword = configer.getProperty("email.password");
		String mailSubject = configer.getProperty("email.subject");
		//SendHtmlMail sendHtmlMail = new SendHtmlMail(mailTo,mailFrom,smtpServer,mailPort,mailProxySet,mailSocksProxyHost,mailSocksProxyPort,mailUserName,mailPassword,mailSubject,urlContent);
		//sendHtmlMail.sendMail();
		
		return null;
	}
	public String getDesSelectedColor() {
		String result = "";
		if (null != this.getSelectedColor() && !("".equals(this.getSelectedColor()))) {
			result = DESPlusUtil.encrypt(this.getSelectedColor());
		}
		return result;
	}
}
